<?php 

declare(strict_types=1);

namespace WanZ\App\Utils\Prometheus;

use Prometheus\Exception\MetricsRegistrationException;
use Prometheus\Histogram;
use Throwable;
use Hyperf\Utils\ApplicationContext;
use Hyperf\HttpServer\Contract\RequestInterface;

class StatisticsRequest extends Prometheus
{
    /**
     * 统计每个接口的请求次数
     * @param string $reqPath
     */
    public function statisticsRequest(string $reqPath)
    {
        if ($reqPath == '/common/get-metrics' || $reqPath == '/user/status' || $reqPath == '/') {
            return;
        }

        $referer = ApplicationContext::getContainer()->get(RequestInterface::class)->header('referer');
        $labels = [
            'req_path' => $reqPath,
        ];

        $labels = array_merge($this->getCompanyIdAndUType(), $labels);
        $this->incBy('statistics', 'request_total', '统计每个接口的请求次数', $labels);
    }

    /**
     * 初始化请求
     * @param $namespace
     * @param $name
     * @param $help
     * @param $labels
     * @param null $quantiles
     * @return Histogram
     * @throws MetricsRegistrationException
     * @throws Throwable
     */
    public function initHistogram($namespace, $name, $help, $labels, $quantiles = null): \Prometheus\Histogram
    {
        return $this->histogram($namespace, $name, $help, $labels, $quantiles);
    }
}
